package com.supinfo.suptracking.servlet;

import com.supinfo.suptracking.entity.User;
import com.supinfo.suptracking.service.UserService;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Fabien, Jérémy, Ludovic, Brice
 */

@WebServlet(name = "ProfileServlet", urlPatterns = {"/profile"})
public class ProfileServlet extends HttpServlet {
    
    @EJB
    UserService userService;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.sendRedirect(getServletContext().getContextPath() + "/jsp/auth/profile.jsp");	
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException { 

        String username = req.getParameter("userName");
        String firstname = req.getParameter("firstName");
        String lastname = req.getParameter("lastName");
        String email = req.getParameter("email");
        String password = req.getParameter("password");

        System.out.println("Username : " + username);
        System.out.println("Firstname : " + firstname);
        System.out.println("Lastname : " + lastname);
        System.out.println("Email : " + email);
        System.out.println("Password : " + password);
        
        User userTestUsername = userService.processGetUserByUsername(username);
        User UserTestMail = userService.processGetUserByMail(email);

        if (userTestUsername.getUserName().equals(username)) {
            
            if (UserTestMail == null) {
                userTestUsername.setUserName(username);
                userTestUsername.setFirstName(firstname);
                userTestUsername.setLastName(lastname);
                userTestUsername.setEmail(email);
                userTestUsername.setPassword(password);

                userService.processUpdateUser(userTestUsername);
                req.getSession().setAttribute("userlogged", userTestUsername);
                req.getSession().setAttribute("event", "1");
                req.getSession().setAttribute("message", "User Update Success");
                resp.sendRedirect(req.getServletContext().getContextPath()+"/profile");          
            }
            else {
                req.getSession().setAttribute("event", "0");
                req.getSession().setAttribute("message", "User Update Failed : Email must be unique");
                doGet(req, resp);
            }
        }
        else{
            req.getSession().setAttribute("event", "0");
            req.getSession().setAttribute("message", "User Update Failed");
            doGet(req, resp);
        }
    }
}
